<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 费用
 *
 * @icon fa fa-circle-o
 */
class Cost extends Backend
{

    /**
     * Cost模型对象
     * @var \app\admin\model\Cost
     */
    protected $model = null;
    protected $relationSearch = true;
    protected $searchFields = ['factory.title'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Cost;
        $this->view->assign("typeList", $this->model->typeList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['Productname','Costmode','Factory'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
           // echo $this->model->getLastSql();
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    //  定义一个公共函数select()，如果这个请求是Ajax，则返回index()函数，否则返回view对象的fetch()函数。
    public function select()
    {
        if ($this->request->isAjax()) {
            $product_id = $this->request->get('product_id');
          //echo "kk".$product_id;
            //当前是否为关联查询
            $this->relationSearch = false;
            //设置过滤方法
            $this->request->filter(['strip_tags', 'trim']);
            if ($this->request->isAjax()) {
                //如果发送的来源是Selectpage，则转发到Selectpage
                if ($this->request->request('keyField')) {
                    return $this->selectpage();
                }
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();
                $list = $this->model
                    ->with(['Productname','Costmode','Factory'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
                 //echo $this->model->getLastSql();
                $result = array("total" => $list->total(), "rows" => $list->items());
                return json($result);
            }
            return $this->view->fetch();
        }
        return $this->view->fetch();
    }
    public function selectQr($ids)
    {
        $where['c.id'] = ['in',$ids];
        $list = Db::name("cost")->alias('c')
            ->join("productname p",'p.id = c.product_id','left')
            ->join("factory f",'f.id = c.factory_id','left')
            ->field("p.name product_name,f.title factory_title,c.*")
            ->where($where)
            ->select();
       // echo Db::name("cost")->getLastSql();
        return json($list);

    }

}
